package demo01;

public class Chengxu35 {
    public static void main(String[] args) {
        int[] arr = {10, 20, 30, 40, 50};
        int[] newArr = swap(arr);
        for (int i : newArr) {
            System.out.print(i + " ");
        }
    }

    public static int[] swap(int[] arr) {
        int maxIndex = 0;
        int minIndex = 0;
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > arr[maxIndex]) {
                maxIndex = i;
            }
            if (arr[i] < arr[minIndex]) {
                minIndex = i;
            }
        }
        int[] newArr = new int[arr.length];
        newArr[0] = arr[maxIndex];
        newArr[arr.length - 1] = arr[minIndex];
        for (int i = 1; i < arr.length - 1; i++) {
            newArr[i] = arr[(i == maxIndex) ? 0 : i];
        }
        return newArr;
    }
}
